<template>
  <el-form :size="parameter.size || 'small'" :model="parameter.form" :rules="parameter.rules" ref="ruleForm" :label-width="parameter.labelWidth||'auto'"
           :label-position="parameter.labelPosition">
    <!--  layerStyle 推荐使用grid布局      -->
    <div :class="{'layerStyle': !parameter.layerStyle}" :style="parameter.layerStyle || {}">
      <div v-for="item in parameter.conditionList" v-show="item.displayNone=== undefined || item.displayNone"
           :key="item.name"
           :class="{'elDivStyle': !item.elDivStyle}" :style="item.elDivStyle || {}"
      >
        <el-form-item v-if="item.displayNone=== undefined || item.displayNone"
                      :key="item.key" :label="item.name" :prop="item.key"
                      :label-width="item.labelWidth || ''"
                      :class="{'elFormItemStyle': !item.elFormItemStyle}" :style="item.elFormItemStyle || {}">
          <el-input v-if="item.type === 'input'"
                    :ref="(el)=>item.ref=el"
                    v-model="parameter.form[item.key]"
                    @keyup.enter="()=>item.keyEnter && item.keyEnter(parameter.form[item.key])"
                    @change="()=>item.change?item.change(parameter.form[item.key]):''"
                    :disabled="item.disabled"
                    clearable
                    :placeholder="item.placeholder===undefined?'请输入':item.placeholder"
                    :style="{width: item.width||'100%'}"
          />
          <el-input-number v-if="item.type === 'inputNum'"
                           v-model="parameter.form[item.key]" :min="item.min" :max="item.max"
                           :ref="(el)=>item.ref=el"
                           @change="()=>item.change?item.change(parameter.form[item.key]):''"
                           @keyup.enter="()=>item.keyEnter?item.keyEnter(parameter.form[item.key]):''"
                           :disabled="item.disabled" :controls-position='item.controlsPosition'
                           :controls='Boolean(item.controls)'
                           :style="{width: item.width||'100%'}"
          />
          <el-select
            v-if="item.type === 'select'"
            :ref="(el)=>item.ref=el"
            :loading="item.loading"
            v-model="parameter.form[item.key]"
            placeholder="请选择" :collapse-tags="item.collapseTags"
            :disabled="item.disabled"
            :multiple="item.multiple"
            clearable
            :allow-create="Boolean(item.allowCreate)"
            :filterable="item.allowCreate ? true : item.filterable"
            :remote="Boolean(item.remoteMethod)"
            :remote-method="item.remoteMethod ? (v)=>item.remoteMethod(v) : ()=>{}"
            :style="{width: item.width||'100%'}"
            @change="(v)=>item.onChange && item.onChange(v,parameter,item)"
            @focus="()=>item.focus?item.focus(null,parameter,item):''"
          >
            <el-option v-for="option in item.options" :key="option.value" :label="option.label" :value="option.value"></el-option>
          </el-select>
          <!-- vue2中 format: 'yyyy-MM-dd'    vue3中 format: 'YYYY-MM-DD'  dateType=datetime 可选时间    -->
          <!-- format: 'timestamp', 时间戳格式      -->
          <el-date-picker
            v-if="item.type === 'date-picker'"
            v-model="parameter.form[item.key]"
            :picker-options="item.pickerOptions"
            :value-format="item.format || 'yyyy-MM-dd HH:mm:ss'"
            @change="()=>item.change && item.change(parameter.form[item.key])"
            @focus="()=>item.focus && item.focus(parameter,item)"
            :type="item.dateType || 'date'"
            :disabled="item.disabled"
            placeholder=""
            clearable
            :style="{width: item.width||'100%' }"
          ></el-date-picker>
          <el-date-picker
            v-if="item.type === 'datePickerRange'"
            v-model="parameter.form[item.key]"
            placeholder=""
            :value-format="item.format || 'yyyy-MM-dd HH:mm:ss'"
            :disabled="item.disabled"
            :style="{width: item.width||'100%' }"
            type="daterange"
            range-separator="至"
            :default-time="['00:00:00', '23:59:59']"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>

          <el-time-picker
            v-if="item.type === 'time-picker'"
            placeholder="选择时间"
            v-model="parameter.form[item.key]"
            :disabled="item.disabled"
            :style="{width: item.width||'100%' }"
          ></el-time-picker>

          <el-switch v-if="item.type === 'switch'"
                     v-model="parameter.form[item.key]"
                     :disabled="item.disabled"
          ></el-switch>

          <el-checkbox-group v-if="item.type === 'checkbox-group'"
                             v-model="parameter.form[item.key]"
                             :disabled="item.disabled">
            <el-checkbox v-for="option in item.options" :label="option" :key="option"></el-checkbox>
          </el-checkbox-group>

          <el-radio-group v-if="item.type === 'radio-group'"
                          v-model="parameter.form[item.key]"
                          :disabled="item.disabled">
            <el-radio v-for="option in item.options" :label="option.value" :key="option.value">{{ option.label }}</el-radio>
          </el-radio-group>

          <el-input
            v-if="item.type === 'textarea'"
            type="textarea"
            :placeholder="item.placeholder===undefined?'请输入':item.placeholder"
            v-model="parameter.form[item.key]"
            :disabled="item.disabled"
            :style="{width: item.width||'100%'}"
          ></el-input>

          <el-cascader v-if="item.type === 'cascader'"
                       v-model="parameter.form[item.key]"
                       :style="{width: item.width||'100%'}"
                       :options="item.options"
                       :disabled="item.disabled"
                       :props="item.props"
                       @change="()=>item.onChange?(v)=>item.onChange(v,parameter):''"
                       @focus="()=>item.focus?()=>item.focus():''"
          ></el-cascader>

          <el-upload v-if="item.type === 'upload'"
                     :style="{width: item.width||'100%'}"
                     :action="item.action||'#'" :headers="item.headers"
                     :limit="item.limit" :file-list="item.fileList"
                     list-type="picture-card"
                     :disabled="item.disabled"
                     :on-change="(file, fileList)=>item.handleChange(file, fileList)"
                     :on-success="(response,file, fileList)=>item.handleSuccess(response,file, fileList)"
                     :auto-upload="Boolean(item.action)">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url" alt=""
              >
              <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="item.handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!item.disabled"
                class="el-upload-list__item-delete"
                @click="item.handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
      </div>
          </el-upload>
          <slot v-if="item.type === 'slot'" :name="item.key" :slotitem="item"/>
        </el-form-item>
      </div>
      <div :class="{'btnLayerStyle': !parameter.btnLayerStyle}" :style="parameter.btnLayerStyle || {}" v-if="parameter.btnList">
        <el-button size="small" v-for="item in parameter.btnList" 
                   :key="item.name" 
                   :type="item.type" 
                   @click="item.action($refs.ruleForm)"
                   :disabled="item.disabled"
                   :loading="item.loading"  :style="item.style || {}" :icon="item.iconLeft">
          {{ item.name }}<i v-if="item.iconRight" :class="item.iconRight"/>
        </el-button>
      </div>
    </div>
<!--    <Form :parameter="parameter">-->
<!--      <template #trayCode2="{slotitem}">-->
<!--        {{slotitem}}-->
<!--      </template>-->
<!--    </Form>-->
  </el-form>
</template>
<script>
export default {
  props: {
    parameter: {
      type: Object,
      required: true,
      default: ()=>({
        size: 'small',
        conditionList: [
          { key: 'maxVolume', type: 'input', name: '最大容积',  },
          { key: 'maxWeight', type: 'input', name: '最大载重',  },
          { key: 'bg', type: 'slot', name: '', elDivStyle: { 'grid-column': '1 / -1' } },
        ],
        form:{ },
        rules: {
          model: [
            { required: true, message: '请输入', trigger: 'blur' },
          ],
          length: [
            { required: true, message: '请输入', trigger: 'blur' },
          ],
          maxVolume: [
            { required: true, message: '请输入', trigger: 'blur' },
          ],
          maxWeight: [
            { required: true, message: '请输入', trigger: 'blur' },
          ],
        },
        layerStyle: { display: 'grid','grid-template-columns': 'repeat(auto-fill, minmax(400px, 1fr))', 'grid-gap': '20px 5px', padding: '0 0 20px 0' },
        btnLayerStyle: { width: '100%',display: 'flex','flex-wrap': 'wrap',gap: '20px 5px','justify-content': 'center' },
        btnList: [
          {
            type: 'text', iconRight:'el-icon-arrow-down',iconLeft:'',
            name: '更多',style:{'margin-left': '6px'},
            action: (ruleForm) => {
              ruleForm.validate((valid) => {
                if (valid) {
                  alert('submit!');
                } else {
                  console.log('error submit!!');
                  return false;
                }
              });
            },
          },
          {
            type: 'text',iconRight:'el-icon-arrow-up',iconLeft:'',
            name: '收起',style:{'margin-left': '6px'},
            action: (ruleForm) => {

            },
          },
        ],
      }),
    },
  },
  data(){
    return{
    }
  },
  methods:{
    validate(){
      return new Promise((resolve,reject) => {
        this.$refs.ruleForm.validate((valid)=>{
          //这里是一个promise的函数
          resolve(valid)
        })
      })
    },
  }
};
</script>
<style scoped lang="scss">
.layerStyle {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-gap: 0 5px;
}
.elDivStyle{
}
.elFormItemStyle{
  margin-bottom: 0px!important;
}
.btnLayerStyle{
  display: flex;
  flex-wrap: wrap;
  gap: 20px 5px;
}
</style>
